﻿using PeshoMVC.Areas.Test.Models;
using PeshoMVC.MyHelpers;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PeshoMVC.Factories
{
    public static class TestFactory
    {
        private static int? questionCount = 1;

        public static int? QuestionCount
        {
            get { return questionCount; }
            set { questionCount = value; }
        }

        public static Dictionary<int?, string> UserAnswers = new Dictionary<int?, string>();
        public static List<int?> listToColorButtons = new List<int?>();

        public static TestModel GetTestModelFromFactory()
        {
            TestModel model = new TestModel()
            {
                AllQuestionsCount = 20,
                QuestionNumber = QuestionCount,
                Name = Data.NameOfQuestions(QuestionCount),
                Answers = Data.QuestionAnswers(QuestionCount),
                SelectedAnswer = CheckIfCurrentQuestionhHasAnswer(UserAnswers)
            };

            return model;
        }

        public static void GetCurrentQuestionNumber(string buttonPrev, string buttonNext, int allQuestionsNumber)
        {
            if (buttonPrev != null && QuestionCount != 1)
            {
                QuestionCount--;
            }
            else if (buttonNext != null && QuestionCount != allQuestionsNumber)
            {
                QuestionCount++;
            }
        }

        public static void SaveAnswers(string value)
        {
            if (value != null)
            {
                if (!UserAnswers.Keys.Contains(QuestionCount))
                {
                    UserAnswers.Add(QuestionCount, value);
                    listToColorButtons.Add(QuestionCount);
                }
                else
                {
                    foreach (var item in UserAnswers)
                    {
                        if (item.Key == QuestionCount)
                        {
                            UserAnswers.Remove(item.Key);
                            UserAnswers.Add(QuestionCount, value);
                            listToColorButtons.Add(QuestionCount);
                            break;
                        }
                    }
                }
            }
        }

        public static string CheckIfCurrentQuestionhHasAnswer(Dictionary<int?, string> userAnswers)
        {
            string existAnswer = string.Empty;

            foreach (var item in userAnswers)
            {
                if (QuestionCount == item.Key)
                {
                    existAnswer = item.Value;
                    break;
                }
            }

            return existAnswer;
        }

        // user it to save data in base
        public static string PutUserAnswesInString(Dictionary<int?, string> userAnswers)
        {
            StringBuilder sb = new StringBuilder();

            if (userAnswers.Count == 0)
            {
                return null;
            }

            foreach (var item in userAnswers)
            {
                sb.AppendFormat("{0}-{1}, ", item.Key, item.Value);
            }
            // remove last ", "
            sb.Remove(sb.Length - 2, 2);

            return sb.ToString();
        }

        public static Dictionary<int?, string> GetCorrectAnswersFromBase(int fromQuestion, int toQuestion)
        {
            Dictionary<int?, string> correctAnswers = new Dictionary<int?, string>();
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                var all =
                    (from q in db.Questions
                     where (q.QuestionId >= fromQuestion && q.QuestionId <= toQuestion)
                     select q.CorrectAnswers).ToList();

                foreach (var item in all)
                {
                    string[] jm = item.Split('-');
                    correctAnswers.Add(int.Parse(jm[0]), jm[1]);
                }

                return correctAnswers;
            }
        }

        public static int CompareUserAnswersWithCorrect(Dictionary<int?, string> userAnswers, Dictionary<int?, string> correctAnswers)
        {
            int result = 0;

            foreach (var userA in userAnswers)
            {
                foreach (var correctA in correctAnswers)
                {
                    if (userA.Key == correctA.Key)
                    {
                        if (userA.Value == correctA.Value)
                        {
                            result++;
                            break;
                        }
                    }
                }
            }

            return result;
        }

        public static bool SaveUserResult(string userName, string userAnswers, string result)
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                var bla =
                       (from u in db.aspnet_Users
                        where u.UserName == userName
                        select u.UserId).ToList();

                db.TestResults.InsertOnSubmit(new TestResult
                {
                    UserId = bla[0],
                    UserAnswers = userAnswers,
                    Result = result.ToString()
                });
                try
                {
                    db.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                } 
            }
        }
    }
}